package com.zd.commodity.productMng.biz.impl;

import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import utils.ExcelUtil;

import com.zd.commodity.biz.impl.BaseBiz;
import com.zd.commodity.productMng.biz.IProductAttrMappingBiz;
import com.zd.commodity.productMng.dao.IProductAttrMappingDao;
import com.zd.commodity.productMng.entity.ProductAttrMapping;
import com.zd.commodity.supplierMng.dao.ISupplierDao;
import com.zd.commodity.supplierMng.entity.Supplier;
@Service
public class ProductAttrMappingBiz extends BaseBiz<ProductAttrMapping>
		implements IProductAttrMappingBiz {
	private IProductAttrMappingDao productAttrMappingDao;
	@Autowired
	private ISupplierDao supplierDao;

	public IProductAttrMappingDao getProductAttrMappingDao() {
		return productAttrMappingDao;
	}
	@Autowired
	public void setProductAttrMappingDao(
			IProductAttrMappingDao productAttrMappingDao) {
		this.setBaseDao(productAttrMappingDao);
		this.productAttrMappingDao = productAttrMappingDao;
	}
	@Override
	public String addMappingByExcel() {
		StringBuilder message = new StringBuilder();
		List<String[]> excelData = readExecl(new HashMap<Integer,String>());
		//校验excel本身是否有重复行
		/*String excelColumRepeat = ExcelUtil.ExcelColumRepeat(excelData, new int[]{});*/
		List<String> list = checkExcelRepeatBatch(excelData,"checkAttrMappingExist", new int[]{3,4,5,6});
		List<String> supplierCheckList = checkExcelRepeatBatch(excelData,"checkSupplierExist", new int[]{3});
		Set<String> supllierSet = new LinkedHashSet();
		for(String[] row : excelData){
			supllierSet.add(row[3]);
		}
		if(supllierSet.size()>supplierCheckList.size()){
			message.append("请先添加以下供应商：");
			supllierSet.remove(supplierCheckList);
			for (String string : supllierSet) {
				message.append(string+",");
			}
			message.deleteCharAt(message.length()-1);
		}
		//校验excel中分类名、属性组名、属性名是否重复
		if(list.size()==0){//数据库没有重复值，进行插入操作
			for (String[] data : excelData) {
				Map<String,String> namesParam = new HashMap();
				namesParam.put("categoryName", data[0]);
				namesParam.put("attrGroupName", data[1]);
				namesParam.put("attrName", data[2]);
				List attrIdList = productAttrMappingDao.getListBySql("findUniqueAttrId", namesParam);
				//保存数据
				Supplier suppliert1 = new Supplier();
				suppliert1.setName(data[3]);
				List<Supplier> supplierQueryList = supplierDao.getList(suppliert1, null, null);
				Supplier supplier = supplierQueryList.get(0);
				ProductAttrMapping attrMapping = new ProductAttrMapping();
				attrMapping.setMAttrId(Long.valueOf(attrIdList.get(0).toString()));
				attrMapping.setSupplier(supplier);
				attrMapping.setCategoryName(data[4]);
				attrMapping.setAttrGroupName(data[5]);
				attrMapping.setAttrName(data[6]);
				productAttrMappingDao.add(attrMapping);
			}
			message.append("导入属性映射成功！");
		}else{
			message.append("以下属性数据库已存在：");
			for (String string : list) {
				message.append(string+",");
			}
			message.deleteCharAt(message.length()-1);
		}
		return message.toString();
	}
	public ISupplierDao getSupplierDao() {
		return supplierDao;
	}
	public void setSupplierDao(ISupplierDao supplierDao) {
		this.supplierDao = supplierDao;
	}

}
